package com.example.gokuplayalong.service.fragments.others;


import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.example.gokuplayalong.R;
import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.beans.Beans;
import com.example.gokuplayalong.bases.custom.UserBaseViewHolder;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.tools.KeyboardsUtils;

import java.util.List;
import java.util.Objects;

public class UserMainPageChatPageViewHolder extends UserBaseViewHolder {
    private final TextView mFragmentUserMainMessageChatPageItemMessageTime;
    private final TextView mFragmentUserMainMessageChatPageItemMessageLeft;
    private final TextView mFragmentUserMainMessageChatPageItemMessageLeftStatus;
    private final TextView mFragmentUserMainMessageChatPageItemMessage;
    private final TextView mFragmentUserMainMessageChatPageItemMessageRightStatus;
    private final TextView mFragmentUserMainMessageChatPageItemMessageRight;
    private final ImageView mFragmentUserMainMessageChatPageItemMessageLeftHeadshot;
    private final ImageView mFragmentUserMainMessageChatPageItemMessageRightHeadshot;
    private final ImageView mFragmentUserMainMessageChatPageItemMessageInnerLeft;
    private final ImageView mFragmentUserMainMessageChatPageItemMessageInnerRight;

    private final LinearLayout mFragmentUserMainMessageChatPageItemMessageLayout;

    private Beans beans = new Beans();

    public UserMainPageChatPageViewHolder(@NonNull View itemView) {
        super(itemView);
        mFragmentUserMainMessageChatPageItemMessageTime = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_time);
        mFragmentUserMainMessageChatPageItemMessageLeft = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_left);
        mFragmentUserMainMessageChatPageItemMessageLeftStatus = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_left_status);
        mFragmentUserMainMessageChatPageItemMessage = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message);
        mFragmentUserMainMessageChatPageItemMessageRightStatus = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_right_status);
        mFragmentUserMainMessageChatPageItemMessageRight = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_right);
        mFragmentUserMainMessageChatPageItemMessageLeftHeadshot = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_left_headshot);
        mFragmentUserMainMessageChatPageItemMessageRightHeadshot = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_right_headshot);
        mFragmentUserMainMessageChatPageItemMessageInnerLeft = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_inner_left);
        mFragmentUserMainMessageChatPageItemMessageInnerRight = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_inner_right);
        mFragmentUserMainMessageChatPageItemMessageLayout = itemView.findViewById(R.id.fragment_user_main_message_chat_page_item_message_layout);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyboardsUtils.hintKeyBoards(v);
            }
        });
        mFragmentUserMainMessageChatPageItemMessageLeftHeadshot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                UserMainProfilePersonPage userMainProfilePersonPage = (UserMainProfilePersonPage) AppParams.getInstance().sysfragmentmanager.findFragmentByTag("UserMainProfilePersonPage");
//                if (userMainProfilePersonPage != null) {
//                    Message message = new Message();
//                    message.what = 1;
//                    Bundle bundle = new Bundle();
//                    bundle.putString("viewObject", AppParams.getInstance().appCurrentUser.getJsonObject().getString("chatObject"));
//                    message.setData(bundle);
//                    userMainProfilePersonPage.handler.sendMessage(message);
//                    SysManagerFragments.switchOnSysFragment("UserMainProfilePersonPage");
//
//                }
            }
        });
    }

    @Override
    public void bindView(List list, Object bean) {
        if (bean instanceof Beans) {
            beans = (Beans) bean;
            initView();
        }
    }

    private void initView() {
        try {
            //            mFragmentUserMainMessageChatPageItemMessage.setTextSize(AppParams.getInstance().appRunParams.getJsonObject().getIntValue("textSize"));
            if (Objects.equals(beans.getJsonObject().getString("send"), AppParams.getInstance().appCurrentUser.getJsonObject().getString("phone"))) {
                mFragmentUserMainMessageChatPageItemMessageTime.setText(String.format("%s %s", beans.getJsonObject().getString("msgStatus"), beans.getJsonObject().getString("timestamp")));
                mFragmentUserMainMessageChatPageItemMessage.setText(JSON.parseObject(beans.getJsonObject().getString("msg")).getString("msg"));
                mFragmentUserMainMessageChatPageItemMessageLeft.setVisibility(View.VISIBLE);
                mFragmentUserMainMessageChatPageItemMessageRight.setVisibility(View.GONE);
                mFragmentUserMainMessageChatPageItemMessageLeftStatus.setVisibility(View.VISIBLE);
                mFragmentUserMainMessageChatPageItemMessageRightStatus.setVisibility(View.GONE);
                mFragmentUserMainMessageChatPageItemMessageLeftHeadshot.setVisibility(View.GONE);
                mFragmentUserMainMessageChatPageItemMessageRightHeadshot.setVisibility(View.VISIBLE);
                mFragmentUserMainMessageChatPageItemMessageInnerLeft.setVisibility(View.GONE);
                mFragmentUserMainMessageChatPageItemMessageInnerRight.setVisibility(View.VISIBLE);
                mFragmentUserMainMessageChatPageItemMessageLayout.setGravity(Gravity.END);
                mFragmentUserMainMessageChatPageItemMessageTime.setGravity(Gravity.END);

//                Picasso.with(MainActivity.getInstance())
//                        .load(AppParams.getInstance().uploadMediaLocationServer + "/" + AppParams.getInstance().appCurrentUser.getJsonObject().getString("userid") + ".png")
////                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
//                        .placeholder(R.drawable.sys_g_all_noimage)
//
//                        .networkPolicy(NetworkPolicy.NO_CACHE)
//                        .into(mFragmentUserMainMessageChatPageItemMessageRightHeadshot);

//                mFragmentUserMainMessageChatPageItemMessageLeftStatus.setText(beans.getJsonObject().getString("msgStatus"));
                mFragmentUserMainMessageChatPageItemMessage.setBackgroundColor(Color.parseColor("#95EC69"));
            }
            else {
                mFragmentUserMainMessageChatPageItemMessageTime.setText(beans.getJsonObject().getString("timestamp"));
                mFragmentUserMainMessageChatPageItemMessage.setText(JSON.parseObject(beans.getJsonObject().getString("msg")).getString("msg"));
                mFragmentUserMainMessageChatPageItemMessageLeft.setVisibility(View.GONE);
                mFragmentUserMainMessageChatPageItemMessageRight.setVisibility(View.VISIBLE);
                mFragmentUserMainMessageChatPageItemMessageLeftStatus.setVisibility(View.GONE);
                mFragmentUserMainMessageChatPageItemMessageRightStatus.setVisibility(View.VISIBLE);
                mFragmentUserMainMessageChatPageItemMessageLeftHeadshot.setVisibility(View.VISIBLE);
                mFragmentUserMainMessageChatPageItemMessageRightHeadshot.setVisibility(View.GONE);
                mFragmentUserMainMessageChatPageItemMessageInnerLeft.setVisibility(View.VISIBLE);
                mFragmentUserMainMessageChatPageItemMessageInnerRight.setVisibility(View.GONE);
                mFragmentUserMainMessageChatPageItemMessageLayout.setGravity(Gravity.START);
                mFragmentUserMainMessageChatPageItemMessageTime.setGravity(Gravity.START);
//                mFragmentUserMainMessageChatPageItemMessageRightStatus.setText(beans.getJsonObject().getString("msgStatus"));
                mFragmentUserMainMessageChatPageItemMessage.setBackgroundColor(Color.WHITE);
//                Picasso.with(MainActivity.getInstance())
//                        .load(AppParams.getInstance().uploadMediaLocationServer + "/" + AppParams.getInstance().appCurrentUser.getJsonObject().getString("chatObject") + ".png")
////                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
//                        .networkPolicy(NetworkPolicy.NO_CACHE)
//                        .placeholder(R.drawable.sys_g_all_noimage)
//
//                        .into(mFragmentUserMainMessageChatPageItemMessageLeftHeadshot);
            }


        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }

    }
}
